package com.gin.no_gay_alliance.fragment.post;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.gin.nga.response.field.TopicKey;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.databinding.FragmentTopicKeyBinding;
import com.gin.no_gay_alliance.databinding.ItemTopicKeyBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.manager.PostManager;
import com.gin.no_gay_alliance.utils.HandlerMessage;

import java.util.ArrayList;

/**
 * 主题分类
 */
public class TopicKeyFragment extends BaseBindingFragment<FragmentTopicKeyBinding> implements View.OnClickListener {
    private static final String ARG_PARAM1 = "forumId";
    private long forumId;
    private BaseBindingRecyclerViewAdapter<ItemTopicKeyBinding, TopicKey> adapter;

    @Override
    protected void onCreateView() {
        if (getArguments() != null) {
            forumId = getArguments().getLong(ARG_PARAM1);
        } else {
            throw new RequireParamException();
        }

        adapter = new BaseBindingRecyclerViewAdapter<ItemTopicKeyBinding, TopicKey>(requireActivity(), new ArrayList<>()) {
            @Override
            protected void onBindViewHolder(ViewHolder<ItemTopicKeyBinding> holder, @NonNull ItemTopicKeyBinding b, int position, TopicKey item) {
                b.tv.setText(item.getName());
                b.iv.setVisibility(item.isSetByModerator() ? View.VISIBLE : View.INVISIBLE);
                b.getRoot().setOnClickListener(TopicKeyFragment.this);
                b.getRoot().setTag(item.getName());
            }

            @NonNull
            @Override
            protected ItemTopicKeyBinding buildBinding(LayoutInflater inflater) {
                return ItemTopicKeyBinding.inflate(inflater);
            }
        };
        b.recyclerView.setAdapter(adapter);
        b.recyclerView.addItemDecoration(new DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL));
        request(false);
    }


    @Override
    public void onClick(View v) {
        //点击时在标题栏插入主题分类
        getParentHandler().sendMessage(getParentHandler().obtainMessage(HandlerMessage.INSERT_TOPIC_KEY, v.getTag()));
    }

    /**
     * 请求主题分类
     *
     * @param ignoreCache 是否忽略缓存
     */
    private void request(boolean ignoreCache) {
        PostManager postManager = MyApplication.app.managerFactory.getPostManager();
        postManager.getTopicKey(ignoreCache, forumId, data -> getParentHandler().post(() -> adapter.updateData(data)));
    }

    public static TopicKeyFragment newInstance(long forumId) {
        TopicKeyFragment fragment = new TopicKeyFragment();
        Bundle args = new Bundle();
        args.putLong(ARG_PARAM1, forumId);
        fragment.setArguments(args);
        return fragment;
    }
}